package io.renren.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.UserService;
import io.renren.modules.xiaoqu.entity.HouseEntity;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/xiaoqu/owner")
public class OwnerController {
    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
        params.replace("page", params.get("page").toString());
        params.replace("limit", params.get("limit").toString());

        PageUtils page = userService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 查询（用户名）
     */
    @PostMapping("/info")
    public R info(int page,int pageSize,String username){

        LambdaQueryWrapper<UserEntity> queryWrapper=new QueryWrapper<UserEntity>().lambda()
                .like(UserEntity::getUsername,username);

        Page<UserEntity> pageInfo=new Page<>(page,pageSize);

        Page<UserEntity> userEntityPage = userService.page(pageInfo, queryWrapper);

        return R.ok().put("user",userEntityPage );
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    public R add(@RequestBody UserEntity userEntity){
        userEntity.setCreateTime(new Date());
        userService.save(userEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity userEntity){
        userService.updateById(userEntity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id){
        userService.delete(id);

        return R.ok();
    }

    /**
     * 批量删除
     */
    @RequestMapping("/deleteall")
    public R deleteAll(@RequestBody Integer[] ids){
        userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
}
